package top.mcwebsite.playandroid.business.message.impl.ui

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Divider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.paging.compose.collectAsLazyPagingItems
import top.mcwebsite.playandroid.business.message.impl.model.MessageModel
import top.mcwebsite.playandroid.business.message.impl.vm.MessageIntent
import top.mcwebsite.playandroid.business.message.impl.vm.MessageTab
import top.mcwebsite.playandroid.business.message.impl.vm.MessageViewModel
import top.mcwebsite.playandroid.common.list.EasyLazyListPageColumn

@Composable
fun MessageContent(
    viewModel: MessageViewModel,
    messageTab: MessageTab
) {
    val lazyPagingItems = when (messageTab) {
        MessageTab.New -> viewModel.unReadMessagePagerFlow
        MessageTab.History -> viewModel.readMessagePagerFlow
    }.collectAsLazyPagingItems()

    EasyLazyListPageColumn(
        lazyPagingItems = lazyPagingItems,
        retry = {
            lazyPagingItems.refresh()
        },
        loadingMoreError = {

        }
    ) { index, messageModel ->
        MessageItem(messageModel = messageModel) { messageModel ->
            viewModel.sendIntent(MessageIntent.OnMessageItemClickIntent(messageModel))
        }
        if (index < lazyPagingItems.itemCount - 1) {
            Divider(
                modifier = Modifier.padding(horizontal = 12.dp, vertical = 5.dp)
            )
        }
    }
}

@Composable
fun MessageItem(
    messageModel: MessageModel,
    onClick: (MessageModel) -> Unit
) {
    Column(
        modifier = Modifier
            .padding(12.dp)
            .clickable { onClick(messageModel) }
    ) {
        Row {
            if (messageModel.tag.isNotEmpty()) {
                Box(
                    modifier = Modifier
                        .border(
                            width = 1.dp,
                            color = MaterialTheme.colorScheme.primary,
                            shape = MaterialTheme.shapes.small
                        )
                        .padding(2.dp)
                ) {
                    Text(
                        text = messageModel.tag,
                        color = MaterialTheme.colorScheme.primary,
                        style = MaterialTheme.typography.labelMedium
                    )
                }
            }
            Text(
                text = messageModel.fromUser,
                style = MaterialTheme.typography.labelMedium,
                modifier = Modifier.padding(start = 5.dp)
            )
            Spacer(Modifier.weight(1f))
            Text(text = messageModel.niceDate, style = MaterialTheme.typography.labelMedium)
        }
        Text(text = messageModel.title, modifier = Modifier.padding(vertical = 2.dp))
        Box(
            modifier = Modifier
                .background(
                    color = MaterialTheme.colorScheme.surfaceVariant,
                    shape = MaterialTheme.shapes.small
                )
                .fillMaxWidth()
                .padding(2.dp)
        ) {
            Text(text = messageModel.message)
        }
    }
}